import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '../store'

Vue.use(VueRouter)


import index from '../pages/index/index.vue'
import Article from "../pages/article/Article";
import snowScene from "../pages/tools/snowScene";

const routes = [
    {
        path: '',
        component: index,
        meta: {
            title: 'Rocco\'s Blog',
            content: {
                keywords: 'Rocco\'s Blog',
                description: '在寻找意义的路上...'
            }
        },

    },
    {
        path: '/article/:articleId',
        name: 'article',
        component: Article,
        props: true
    },
    {
        path: '/classify/:sortName',
        name: 'classify',
        component: resolve => require(['../pages/classify/Classify'], resolve),
        meta: {
            title: '标签&Rocco',
            content: {
                keywords: '标签&Rocco',
                description: '标签分类获取文章'
            }
        },
        props: true
    },
    {
        path: '/Open_urls',
        name: 'openUrl',
        component: resolve => require(['../pages/tools/OpenUrl'], resolve),
        meta: {
            title: '批量打开网站&Rocco',
            content: {
                keywords: '批量打开网站',
                description: '自动打开多个网站网址'
            }
        },
        props: true
    },
    {
        path: '/snow',
        name: 'snow',
        component: resolve => require(['../pages/tools/snowScene'], resolve),
        meta: {
            title: 'the world of snow&Rocco',
            content: {
                keywords: 'snow',
                description: 'the world of snow'
            }
        },
    },
    {
        path: '/games',
        name: 'games',
        component: resolve => require(['../pages/tools/GameFlyWar'], resolve),
    },
];


const router = new VueRouter({
    mode: 'history',
    routes
});


router.beforeEach((to, from, next) => {
    /* 路由发生变化修改页面meta */

    if (to.meta.content) {
        document.querySelector('meta[name="keywords"]').setAttribute('content', to.meta.content.keywords);
        document.querySelector('meta[name="description"]').setAttribute('content', to.meta.content.description)
    }
    /* 路由发生变化修改页面title */
    if (to.meta.title) {
        document.title = to.meta.title;
    }
    /* 为每次路由变化提供遮罩层  */
    if (!store.state.screen) {
        store.commit('negate');
        console.log('this.$store.state.screen in router:' + store.state.screen);
    }

    next()
});

export default router
